package com.idrem.views.visit;

import java.io.File;
import java.lang.reflect.Array;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.LocationListener;
import com.baidu.mapapi.MKGeneralListener;
import com.baidu.mapapi.MKLocationManager;
import com.idrem.R;
import com.idrem.common.Common;
import com.idrem.common.MySharedPreferences;

import android.app.Activity;
import android.app.ActivityGroup;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;
import android.widget.Toast;

public class VisitMainAct extends ActivityGroup {
	// private LocationListener locationListener = (LocationListener) new
	// VisitMainAct();
	String CustImagePath = null;
	private CustomerAdapter adapter_nei;
	private CustomerAdapter adapter_wai;
	private String[][] cust_data_nei;
	private String[][] cust_data_wai;
	private TextView custtitle;
	String dmsid = null;
	String dsrcode = null;
	private ImageListView list_nei;
	private ImageListView list_wai;
	private LinearLayout ll;
	private BMapManager mapManager;
	private MKLocationManager myLocationManager;
	private String sql_nei = null;
	private String sql_wai = null;
	private TabHost tabHost;
	private EditText visit_main_et_search;
	private Double xDouble = Double.valueOf(0.0D);
	private Double yDouble = Double.valueOf(0.0D);

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(1);
		setContentView(R.layout.visitmain);
		this.custtitle = ((TextView) findViewById(R.id.custtitle));
		if (!Settings.Secure.isLocationProviderEnabled(getContentResolver(), "gps"))
			MySharedPreferences.getInstance(this).toggleGPS(this);
		File localFile;
		localFile = new File(Common.getImageFolderPath(this) + "customer/");
		if (!localFile.exists())
			localFile.mkdirs();
		// //GPS Info
		// this.mapManager = new BMapManager(getApplication());
		// this.mapManager.init("52607940E4ADA5625B44AAA693458AF187B47F1E",
		// new VisitMainAct());
		// this.myLocationManager = this.mapManager.getLocationManager();
		// this.myLocationManager.disableProvider(0);
		// this.myLocationManager.disableProvider(1);
		// this.myLocationManager.enableProvider(0);
		// this.myLocationManager.enableProvider(1);
		// this.myLocationManager.requestLocationUpdates(this.locationListener);
		// this.mapManager.start();
		// this.dsrcode =
		// MySharedPreferences.getInstance(this).getDsrCode();
		// this.dmsid = MySharedPreferences.getInstance(this).getDmsId();

		// //get data from local sqlite db
		// this.sql_nei =
		// (" select distinct a.cust_code,b.name,(case when c.visit_id is null then '未拜访' else '已拜访' end) visit_status,b.xx,b.yy,b.type,e.name area_name,f.name type_name   from UserRouteCust a  left join Customer b on b.code = a.cust_code and a.dms_id = b.dms_id  left join HistoryVisit c on a.cust_code = c.cust_code and c.dms_id = a.dms_id and date(c.start_time) = '"
		// + Common.getNowDateStr() + "' " +
		// " left join CustomerArea e on e.code = b.area and e.dms_id = b.dms_id "
		// +
		// " left join Channel f on f.code = b.type and f.dms_id = b.dms_id "
		// + " where date(a.route_date) = '" + Common.getNowDateStr() + "' "
		// + " and a.dsr_code = '" + this.dsrcode + "'" +
		// " and a.dms_id = '" + this.dmsid + "'" +
		// " order by a.cust_code ");
		// this.sql_wai =
		// (" select distinct a.code,a.name,(case when b.visit_id is null then '未拜访' else '已拜访' end) visit_status,a.xx,a.yy ,a.type,e.name area_name,f.name type_name    from Customer a     left join HistoryVisit b on a.code = b.cust_code and b.dms_id = a.dms_id and date(b.start_time) = '"
		// + Common.getNowDateStr() + "'   " +
		// " left join CustomerArea e on e.code = a.area and e.dms_id = a.dms_id "
		// +
		// " left join Channel f on f.code = a.type and f.dms_id = a.dms_id "
		// +
		// "  where a.code not in (select cust_code from UserRouteCust where date(route_date) = '"
		// + Common.getNowDateStr() + "' and dms_id = '" + this.dmsid +
		// "' )   " + "  and a.dms_id = '" + this.dmsid + "' ");

		RelativeLayout localRelativeLayout1 = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.minitab, null);
		((TextView) localRelativeLayout1.findViewById(R.id.tab_label)).setText("线内门店");
		RelativeLayout localRelativeLayout2 = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.minitab, null);
		((TextView) localRelativeLayout2.findViewById(R.id.tab_label)).setText("线外门店");
		RelativeLayout localRelativeLayout3 = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.minitab, null);
		((TextView) localRelativeLayout3.findViewById(R.id.tab_label)).setText("地图");
		this.tabHost = ((TabHost) findViewById(R.id.tabhost));
		this.tabHost.setup();
		this.tabHost.addTab(this.tabHost.newTabSpec("nei").setIndicator(localRelativeLayout1).setContent(R.id.visitmain_list_nei));
		this.tabHost.addTab(this.tabHost.newTabSpec("wai").setIndicator(localRelativeLayout2).setContent(R.id.visitmain_list_wai_ll));
		this.tabHost.addTab(this.tabHost.newTabSpec("map").setIndicator(localRelativeLayout3).setContent(R.id.visitmain_map));
		this.tabHost.setCurrentTab(0);

		//in-line
		this.list_nei = ((ImageListView) findViewById(R.id.visitmain_list_nei));
		this.cust_data_nei = getData(this.sql_nei);
		this.adapter_nei = new CustomerAdapter(this, this.cust_data_nei, this.xDouble.doubleValue(), this.yDouble.doubleValue());
//		this.list_nei.setAdapter(this.adapter_nei);//error why?
//		//click in-line list item
//		this.list_nei.setOnClickListener(new View.OnClickListener() {
//
//			public void onClick(View v) {
//				Toast.makeText(getApplicationContext(), "list_nei_item clicked,goto sign page", Toast.LENGTH_SHORT).show();
//			}
//		});
		
		//out-line
		this.list_wai = ((ImageListView) findViewById(R.id.visitmain_list_wai));
		this.cust_data_wai = getData(this.sql_wai);
		this.adapter_wai = new CustomerAdapter(this, this.cust_data_wai, this.xDouble.doubleValue(), this.yDouble.doubleValue());
//		this.list_wai.setAdapter(this.adapter_wai);//error why?
//		//click in-line list item
//		this.list_wai.setOnClickListener(new View.OnClickListener() {
//
//			public void onClick(View v) {
//				Toast.makeText(getApplicationContext(), "list_wai_item clicked,goto sign page", Toast.LENGTH_SHORT).show();
//			}
//		});	
		//search edit text item
		this.visit_main_et_search = ((EditText) findViewById(R.id.visit_main_et_search));
		this.visit_main_et_search.addTextChangedListener(new TextWatcher() {

			public void onTextChanged(CharSequence s, int start, int before, int count) {
				Toast.makeText(getApplicationContext(), "visit_main_et_search onTextChanged", Toast.LENGTH_SHORT).show();
				// start search?
			}

			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
				Toast.makeText(getApplicationContext(), "visit_main_et_search beforeTextChanged", Toast.LENGTH_SHORT).show();
				// start search?

			}

			public void afterTextChanged(Editable s) {
				Toast.makeText(getApplicationContext(), "visit_main_et_search afterTextChanged", Toast.LENGTH_SHORT).show();
				// start search?
			}
		});
		
		//tab-changed
		this.tabHost.setOnTabChangedListener(new OnTabChangeListener() {

			public void onTabChanged(String tabId) {
				Toast.makeText(getApplicationContext(), "tabHost OnTabChangedListener", Toast.LENGTH_SHORT).show();
			}
		});
	}

	private String[][] getData(String sql_nei2) {
		// get data from local sqlite db
		return null;
	}

	@Override
	protected void onDestroy() {
		// close Map Manager
		if (this.mapManager != null) {
			this.mapManager.stop();
			this.mapManager.destroy();
			this.mapManager = null;
		}
		super.onDestroy();
	}

	@Override
	protected void onResume() {
		super.onResume();
		String str = this.tabHost.getCurrentTabTag();
		if (str.equals("nei")) {
			this.tabHost.setCurrentTab(0);
			// // update visit in-line customer and refresh Adapter
			// this.cust_data_nei = null;
			// this.cust_data_nei = getData(this.sql_nei);
			// if (this.cust_data_nei == null)
			// {
			// int[] arrayOfInt2 = new int[2];
			// arrayOfInt2[0] = 0;
			// arrayOfInt2[1] = 0;
			// this.cust_data_nei = ((String[][])Array.newInstance(String.class,
			// arrayOfInt2));
			// }
			// new Handler().post((Runnable) new VisitMainAct());
			// this.list_nei.refreshAdapter();
		}

		if (str.equals("wai")) {
			this.tabHost.setCurrentTab(1);
			// //update visit out-line customer and refresh Adapter
			// this.cust_data_wai = null;
			// this.cust_data_wai = getData(getQuerySql(this.sql_wai));
			// if (this.cust_data_wai == null)
			// {
			// int[] arrayOfInt1 = new int[2];
			// arrayOfInt1[0] = 0;
			// arrayOfInt1[1] = 0;
			// this.cust_data_wai = ((String[][])Array.newInstance(String.class,
			// arrayOfInt1));
			// }
			// new Handler().post(new VisitMainAct.9(this));
			// this.list_wai.refreshAdapter();
		}

		if (str.equals("map"))
			this.tabHost.setCurrentTab(2);
	}
}
